Remove a part of a string?

Hi all,

i have a .txt with user names and a header

i want to read the user names but not the header

the header is marked with "//" at the line start and has x lines in the .txt

how can i "remove" or "not print" the header?

Need i regex? (i do not hope :D )

Here my code

void readFromFile()
{
        string filename = "C:\\Test.txt"    
        Stream input = read filename
        string str
        int star = 0
        Skip fileLines = create
        
        while (true)
        {
            input >> str
            if(end of input) break
            put(fileLines, star, str)
            star++
        }
        
        for str in fileLines do
        {
            if(str != "//")
            {
                print str "\n"        
            }
        }
        delete fileLines        
}
readFromFile

I'm sry, I'm new in dxl coding


meinhana - Thu Jul 23 08:43:11 EDT 2015

Re: Remove a part of a string?
Bjoern_Karpenstein - Thu Jul 23 09:07:03 EDT 2015

Hi!

You have to ask, if str contains "//" and you can do this with the function

            if(!matches("//",str))
            {
                print str "\n"        
            }

Greetings!

Björn Karpentein

Re: Remove a part of a string?
meinhana - Thu Jul 23 09:09:56 EDT 2015

BEST ANSWER EVER

THANK YOU !!!! I LOVE YOU Grin

Re: Remove a part of a string?
Bjoern_Karpenstein - Thu Jul 23 09:14:30 EDT 2015

meinhana - Thu Jul 23 09:09:56 EDT 2015

BEST ANSWER EVER

THANK YOU !!!! I LOVE YOU Grin

You are welcome darling <3

Re: Remove a part of a string?
meinhana - Fri Jul 24 04:59:15 EDT 2015

Another question,

how can i remove a part of a string array? ( The part with the "//" at the start)

 

string filename = "C:\\Test.txt"    
Stream input = read filename
string str
int star = 0

while (true)
{
    input >> str
    if(end of input) break
    star++
}

string strArray[star]
input = read filename    
star = 0
 
while (true)
{
    input >> str
        if(end of input) break
        strArray[star] = str // !!!!! THIS STRING ARRAY !!! 
        star++
}

 

Re: Remove a part of a string?
Mathias Mamsch - Fri Jul 24 05:42:37 EDT 2015

meinhana - Fri Jul 24 04:59:15 EDT 2015

Another question,

how can i remove a part of a string array? ( The part with the "//" at the start)

 

string filename = "C:\\Test.txt"    
Stream input = read filename
string str
int star = 0

while (true)
{
    input >> str
    if(end of input) break
    star++
}

string strArray[star]
input = read filename    
star = 0
 
while (true)
{
    input >> str
        if(end of input) break
        strArray[star] = str // !!!!! THIS STRING ARRAY !!! 
        star++
}

 

To answer your question: You can shift the elements inside the array and though overwrite the elements you do not want to keep, e.g. like this (which is rather complicated -> see below):

int removed = 0; 
int i; for (i = 0; i < sizeof strArray; i++) {
    bool keep = ... // add your condition to keep the element or not , eg. str contains
    
    if (!keep) { 
        removed++; // this element will go away so we need to shift the next elements
    } else {
        // shift the current element in the array left ...
        if (removed > 0) strArray[i-removed] = strArray[i];
    }
    if (removed > 0) strArray[i] = null // clear the shifted element ...
}

Note the the overall Array length will not be smaller - but will only contains lesser elements.

 

Still: Why would you need to buffer the whole file inside an array? Reading the file twice is overkill. You should keep your Skip list approach if it is necessary to cache the whole file. You can delete easily from a skip list without the need to shift the elements.

And in most cases you can get away without even needing to store the lines, directly processing them.

Hope this helps, regards, Mathias

 

Re: Remove a part of a string?
meinhana - Fri Jul 24 06:43:53 EDT 2015

Mathias Mamsch - Fri Jul 24 05:42:37 EDT 2015

To answer your question: You can shift the elements inside the array and though overwrite the elements you do not want to keep, e.g. like this (which is rather complicated -> see below):

int removed = 0; 
int i; for (i = 0; i < sizeof strArray; i++) {
    bool keep = ... // add your condition to keep the element or not , eg. str contains
    
    if (!keep) { 
        removed++; // this element will go away so we need to shift the next elements
    } else {
        // shift the current element in the array left ...
        if (removed > 0) strArray[i-removed] = strArray[i];
    }
    if (removed > 0) strArray[i] = null // clear the shifted element ...
}

Note the the overall Array length will not be smaller - but will only contains lesser elements.

 

Still: Why would you need to buffer the whole file inside an array? Reading the file twice is overkill. You should keep your Skip list approach if it is necessary to cache the whole file. You can delete easily from a skip list without the need to shift the elements.

And in most cases you can get away without even needing to store the lines, directly processing them.

Hope this helps, regards, Mathias

 

To do that ->

textList = listView(mainWindow, 0, 350, 20, strArray); 

The file is a list of all users in my database and i want to show it in a output field.

to your answer, how can i add my condition (like string("//")) in a bool ??? I'm confused

Thanks for your help

Re: Remove a part of a string?
Mathias Mamsch - Fri Jul 24 07:03:19 EDT 2015

meinhana - Fri Jul 24 06:43:53 EDT 2015

To do that ->

textList = listView(mainWindow, 0, 350, 20, strArray); 

The file is a list of all users in my database and i want to show it in a output field.

to your answer, how can i add my condition (like string("//")) in a bool ??? I'm confused

Thanks for your help

A boolean is either true or false. At some point you decide, whether or not to keep the entry ( e.g. contains("//") ... which returns a boolean itself).

 

However In your should use your Skip approach (which is much easier) and then afterwards build an array from the skip list. Because for the GUI you will need an array that is sized to the correct dimensions. And you not add it to the list beforehand. To revive your old code:

Skip readFromFile()
{
        string filename = "C:\\Test.txt"    
        Stream input = read filename
        string str
        int star = 0
        Skip fileLines = create
        
        while (true)
        {
            input >> str
            if(end of input) break
                        if(!matches("//",str)) put (fileLines, star++, str); 
        }
        close (input); 
        return fileLines; 
}


void buildGUI () {
        Skip fileLines = readFromFile(); 
    
        int i = 0; 
        
        for str in fileLines do i++; // count the skip entries 
              
        string sArLines [i];   // declare array in correct size
        
        // convert skip to array
        i = 0; for str in fileLines do sArLines[i++] = str; 
        
        delete fileLines      
        
        // build up your GUI here ...
}

buildGUI ()

 

Re: Remove a part of a string?
meinhana - Fri Jul 24 07:34:45 EDT 2015

Thank you! It works